package org.example.gradleauth.security.event;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.context.event.EventListener;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent;
import org.springframework.stereotype.Component;

/**
 * 监听器捕获列表 参考
 * org.springframework.security.authentication.event.AbstractAuthenticationEvent
 *
 * providerManager.setAuthenticationEventPublisher(defaultAuthenticationEventPublisher);
 *
 * @author 1141193930@qq.com
 */
@Slf4j
@Component
public class LoginFailureEvent {

    @EventListener
    public void handleBadCredentials(AuthenticationFailureBadCredentialsEvent event) {

        if (event.getException() instanceof BadCredentialsException) {
            String username = event.getAuthentication().getName();
            log.error("⚠️ 登录失败 - 用户名：{} exception：{}", username, ExceptionUtils.getStackTrace(event.getException()));
        }

    }
}